package com.qingcheng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.pojo.system.Admin;
import com.qingcheng.service.system.AdminService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: LDeng
 * @Date: 2020-04-20 17:12
 */
public class UserDetailsServiceImpl implements UserDetailsService {

    @Reference
    private AdminService adminService;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        //返回UserDetails 接口的实现类 User()
        //List<GrantedAuthority> 权限集合
        System.out.println("Debug============>UserDetailService Impl");
       //构建查询条件
        Map map=new HashMap<>();
        map.put("loginName",s);
        map.put("status","1");
        //查询出Admin对象
        List<Admin> list = adminService.findList(map);
        if(list.size()==0){//没查到
            return null;
        }
        List<GrantedAuthority> grantedAuthorities=new ArrayList<GrantedAuthority>();
        grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));//实际是查询数据库
        return new User(s,list.get(0).getPassword(),grantedAuthorities);
    }
}
